package cn.snmp.mutual.nms.mibs;

import java.io.*;
import java.util.*;
import cn.snmp.mutual.nms.SnmpOID;

public class MibModule implements MIBConstants, Serializable
{
    protected Hashtable m_propreties;
    protected Vector m_nodes;
    protected Vector m_traps;
    protected Vector m_tcs;
    protected MibOperations m_options;
    
    protected void setMibTraps(final Vector vTraps) {
        this.m_traps = vTraps;
    }
    
    public Enumeration getDefinedTraps() {
        if (this.m_traps == null) {
            return null;
        }
        return this.m_traps.elements();
    }
    
    protected void setMibTCs(final Vector vTCs) {
        this.m_tcs = vTCs;
    }
    
    public Enumeration getDefinedTCs() {
        if (this.m_tcs == null) {
            return null;
        }
        return this.m_tcs.elements();
    }
    
    public Enumeration getDefinedNodes() {
        return this.m_nodes.elements();
    }
    
    public MibModule(final MibOperations options, final Hashtable propreties, final Vector nodes) {
        this.m_propreties = null;
        this.m_nodes = null;
        this.m_traps = null;
        this.m_tcs = null;
        this.m_options = null;
        this.m_options = options;
        this.m_propreties = propreties;
        this.m_nodes = nodes;
    }
    
    public Hashtable getProprities() {
        return this.m_propreties;
    }
    
    public MibOperations getMibOperations() {
        return this.m_options;
    }
    
    public Vector getMibNodes() {
        return this.m_nodes;
    }
    
    @Override
    public String toString() {
        return (String)this.m_propreties.get("Name");
    }
    
    public void clear() {
        this.m_propreties.clear();
        for (int i = 0; i < this.m_nodes.size(); ++i) {
            final MibNode node = (MibNode)this.m_nodes.get(i);
            node.clear();
        }
        this.m_nodes.clear();
        this.m_propreties = null;
        this.m_nodes = null;
        this.m_options = null;
    }
    
    public MibNode getMibNode(final String name) {
        if (name == null) {
            return null;
        }
        for (int i = 0; i < this.m_nodes.size(); ++i) {
            final MibNode node = (MibNode)this.m_nodes.get(i);
            if (name.equals(node.getLabel())) {
                return node;
            }
        }
        return null;
    }
    
    public MibNode getMibNode(final SnmpOID oidObject) {
        if (oidObject == null) {
            return null;
        }
        final int[] numbers = oidObject.toIntArray();
        for (int i = 0; i < this.m_nodes.size(); ++i) {
            final MibNode node = (MibNode)this.m_nodes.get(i);
            final SnmpOID tempOid = new SnmpOID(node.getNumberedOIDString());
            final int[] tempNumbers = tempOid.toIntArray();
            boolean isOther = false;
            if (numbers.length >= tempNumbers.length) {
                for (int j = 0; j < tempNumbers.length; ++j) {
                    if (tempNumbers[j] != numbers[j]) {
                        isOther = true;
                        break;
                    }
                }
                if (!isOther) {
                    if (node.getSyntax() != null) {
                        return node;
                    }
                }
            }
        }
        return null;
    }
}
